package com.threegroup.admin.security.config;

import com.alibaba.fastjson.JSON;
import com.threegroup.common.utils.ServletUtils;
import com.threegroup.common.utils.StringUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 暂无权限处理类
 */
@Component
public class UserAuthAccessDeniedHandler implements AccessDeniedHandler {
    /**
     * 暂无权限返回结果
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        Map<String, Object> result = new HashMap<>(3);
        result.put("code", 403);
        String msg = StringUtils.format("请求访问：{}，未授权，无法访问系统资源", request.getRequestURI());
        result.put("msg", msg);
        result.put("data", "暂无权限，请联系管理员");
        ServletUtils.renderString(response, JSON.toJSONString(result));
    }
}
